package com.sun.lotter;

import java.sql.Date;
import java.util.ArrayList;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.sun.base.Ball;
import com.sun.base.BasePlayType;
import com.sun.bata.LotterData;
import com.sun.bata.SQLlite;
import com.tools.SysInterfacePhone;

public class LotterNumberDialog extends Dialog implements OnClickListener{
	LinearLayout ballLayout;
	ArrayList<String>lotternumbers;
	int lotterId;
	int playID;
	public BasePlayType playType;
	public LotterNumberDialog(Context context,ArrayList<String>lotternumbers,int lotterId,int playID) {
		super(context);
		
		this.lotternumbers = lotternumbers;
		this.lotterId = lotterId ;
		this.playID = playID;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.luckmachine_number_list, null);		
		view.findViewById(R.id.Button_SettingBet).setOnClickListener(this);
		FrameLayout v = (FrameLayout) view
				.findViewById(R.id.FrameLayout_LotterNumberList);
		ballLayout = new LinearLayout(context);
		initBall();
		v.addView(ballLayout);	
		this.setContentView(view,new LayoutParams(-1,-2));
		this.setTitle("您的幸运号：");
	
	}
public void onCreate(Bundle bundle){
	WindowManager.LayoutParams lp=this.getWindow().getAttributes();
	lp.width=(int) (SysInterfacePhone.invoker().getScreenWidth(getContext())*0.99);
	this.getWindow().setAttributes(lp);
	super.onCreate(bundle);
	
}
	private void initBall() {

		String ballid[];
		switch (lotterId) {
		case LotterData.DOUBLECOLORBALL:// 双色球
			ballid = new String[7];
			for (int i = 0; i < ballid.length; i++) {
				ballid[i] = lotternumbers.get(0).substring(i * 3,
						i * 3 + 2);
			}
			loadballbround(6, Ball.REDBALL, 1, Ball.BULEBALL, R.color.white,
					ballid);
			break;
		case LotterData.FREQLOTT:// 时时彩
			switch (playID) {
			case 0:// 五星通选
			case 1:// 五星玩法
			case 2:// 三星玩法
			case 3:// 二星玩法
			case 4:// 一星玩法
				ballid = new String[5];
				break;
			case 5:// 大小单双
			case 6:// 二星组选
				ballid = new String[2];
				break;
			default:
				ballid = new String[0];
				break;
			}
			for (int i = 0; i < ballid.length; i++) {
				ballid[i] = lotternumbers.get(0).substring(i * 2, i * 2 + 1);
			}
			loadballbround(ballid.length, Ball.YELLOWBALL, 0, Ball.BULEBALL,
					R.color.black, ballid);
			break;
		case LotterData.SELECT5IN11:// １１选５
			ballid = new String[lotternumbers.get(0).length() / 3 + 1];
			for (int i = 0; i < ballid.length; i++) {
				ballid[i] = lotternumbers.get(0).substring(i * 3,
						i * 3 + 2);
			}
			loadballbround(ballid.length, Ball.YELLOWBALL, 0, Ball.BULEBALL,
					R.color.black, ballid);
			break;
		case LotterData.SUPERHAPPY:// 大乐透
			switch (playID) {
			case 0:
				ballid = new String[7];
				for (int i = 0; i < ballid.length; i++) {
					ballid[i] = lotternumbers.get(0).substring(i * 3,
							i * 3 + 2);
				}
				loadballbround(5, Ball.REDBALL, 2, Ball.BULEBALL,
						R.color.white, ballid);
				break;
			case 1:
				ballid = new String[2];
				for (int i = 0; i < ballid.length; i++) {
					ballid[i] = lotternumbers.get(0).substring(i * 3,
							i * 3 + 2);
				}
				loadballbround(2, Ball.YELLOWBALL, 0, Ball.BULEBALL,
						R.color.black, ballid);
				break;
			default:
				break;
			}
			break;
		case com.sun.bata.LotterData.WELFARE3D:// 福彩3D
			ballid = new String[lotternumbers.get(0).length() / 2 + 1];
			for (int i = 0; i < ballid.length; i++) {
				ballid[i] = lotternumbers.get(0).substring(i * 2,
						i * 2 + 1);
			}
			loadballbround(3, Ball.YELLOWBALL, 0, Ball.BULEBALL, R.color.black,
					ballid);
			break;
		default:
			break;
		}

	
	}
	private void loadballbround(int rednumber, int redBallResID,
			int bulenumber, int buleBallResID, int textColorResID,
			String[] ballID) {
		Ball[] ball = new Ball[rednumber + bulenumber];
		ballLayout.setGravity(Gravity.CENTER_HORIZONTAL);
		for (int i = 0; i < rednumber; i++) {
			ball[i] = new Ball(getContext(), redBallResID, null, i,lotterId,playID);
			ball[i].setClickable(false);
			ball[i].setChecked(true);
			ballLayout.addView(ball[i]);
		}
		for (int i = 0; i < bulenumber; i++) {
			ball[i + rednumber] = new Ball(getContext(), buleBallResID, null, i
					+ rednumber,lotterId,playID);
			ball[i + rednumber].setClickable(false);
			ball[i + rednumber].setChecked(true);
			ballLayout.addView(ball[i + rednumber]);
		}
		for (int i = 0; i < ball.length; i++) {
			ball[i].setBallLable(ballID[i]);
		}
	}

	@Override
	public void onClick(View v) {
		Date date=  new Date(System.currentTimeMillis());
		String time = (date.toLocaleString());
		String lotterName = LotterData.getlotName(lotterId);
		String lotterNumberString = lotternumbers.get(0);
		String tabName = LotterData.getLotEName(lotterId);
		
		SQLlite db = new SQLlite(getContext());
		ContentValues values = new ContentValues();
		values.put("id", db.rawQuery(tabName).getCount());
		values.put("time", time);
		values.put("lotterName", lotterName);
		values.put("lotterplay", "直选");
		values.put("lotterNumbers", lotterNumberString);	
		db.close();

		db.insert(LotterData.getLotEName(lotterId), values);
		
		SysInterfacePhone.invoker().createToast(getContext(), "号码已保存,请到'更多'里面查询!").show();
		
		this.cancel();
		if (playType!=null) {
			playType.onEvent("ok");
		}
		
	}
	
}
